package shenxg.game;

import java.awt.Color;
import java.util.ArrayList;

public class Item {
	private int position_x;
	private int position_y;
	public final OriginalItem original;
	public final Scene scene;
	public ItemGroup itemGroup = null;

	public Item(int x, int y, Color color, Scene scene) throws Exception {
		position_x = x;
		position_y = y;
		original = new OriginalItem(x, y, color);
		this.scene = scene;
	}

	public Item(int x, int y, Item pre_item, Scene scene) {
		position_x = x;
		position_y = y;
		original = pre_item.original;
		this.scene = scene;
	}

	public Item clone(Scene scene) {
		return new Item(position_x, position_y, this, scene);
	}

	public static boolean isCorrectPoint(int x, int y) {
		if (x >= Scene.X_LENGTH || x < 0 || y >= Scene.Y_LENGTH || y < 0) {
			return false;
		}
		return true;
	}

	public ArrayList<Item> getNearestSameColorItems() {
		ArrayList<Item> items = new ArrayList<Item>();
		// upper
		if (this.position_y > 0 && null != this.scene.items[this.position_x][this.position_y - 1]
				&& this.scene.items[this.position_x][this.position_y - 1].getColorType() == this.getColorType()) {
			items.add(this.scene.items[this.position_x][this.position_y - 1]);
		}
		// down
		if (this.position_y < Scene.Y_LENGTH - 1 && null != this.scene.items[this.position_x][this.position_y + 1]
				&& this.scene.items[this.position_x][this.position_y + 1].getColorType() == this.getColorType()) {
			items.add(this.scene.items[this.position_x][this.position_y + 1]);
		}
		// left
		if (this.position_x > 0 && null != this.scene.items[this.position_x - 1][this.position_y]
				&& this.scene.items[this.position_x - 1][this.position_y].getColorType() == this.getColorType()) {
			items.add(this.scene.items[this.position_x - 1][this.position_y]);
		}
		// right
		if (this.position_x < Scene.X_LENGTH - 1 && null != this.scene.items[this.position_x + 1][this.position_y]
				&& this.scene.items[this.position_x + 1][this.position_y].getColorType() == this.getColorType()) {
			items.add(this.scene.items[this.position_x + 1][this.position_y]);
		}

		return items;
	}

	public ColorType getColorType() {
		return original.colorType;
	}

	public int getX() {
		return position_x;
	}

	public void setX(int position_x) {
		this.position_x = position_x;
	}

	public int getY() {
		return position_y;
	}

	public void setY(int position_y) {
		this.position_y = position_y;
	}

	public void moveTo(int x, int y) throws Exception {
		if (x == this.position_x && y == this.position_y)
			return;
		if (x < 0 && x > Scene.X_LENGTH-1 && y < 0 && y > Scene.Y_LENGTH -1)
			throw new Exception("Error Position X : " + x + "Y : " + y);
		if (null != scene.items[x][y]) {
			throw new Exception("dest position is not null x : " + x + " y : " + y);
		}

		scene.items[position_x][position_y] = null;
		this.position_x = x;
		this.position_y = y;
		scene.items[position_x][position_y] = this;
	}
	
	

}
